//用户vuex模块

import {checkLoginReq} from '@/api/user'
const state = {
  role: localStorage.getItem('role'), //用户角色
  token: localStorage.getItem('t_k'), //用户token
  }
  
  const mutations = {
    SET_TOKEN(state, data) {
      state.token = data
      localStorage.setItem('t_k', data)
    },
    SET_ROLE(state, data) {
      state.role = data
      localStorage.setItem('role', data)
    },
  }
  
  const actions = {
    A_login({commit}, data) {
      return new Promise(async resolve => {
        let res = await checkLoginReq(data)
        let {code, role, token} = res.data
        if (code === 0) {
          commit('SET_TOKEN', 'Bearer ' + token)
          commit('SET_ROLE', role)
          resolve()
        }
      })
    },
  }
  
  //导出模块
  export default {
    namespaced: true,
    state,
    mutations,
    actions,
  }
  